﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Author: ljj
//Date: 20190707
//Function: 升级buff
public class LevelUp : BaseBuff
{

    public override void BuffUp()
    {
        BattleObject bt = gameObject.GetComponent<BattleObject>();
        bt.setMaxHP(Mathf.Floor(bt.maxHP * (1 + effect * 0.25f)));
        bt.damage = Mathf.Floor(bt.damage * (1 + effect * 0.25f));
        UpdateSpeed(1 + effect * 0.1f);
        GameObject starPrefab = Resources.Load("Prefabs/Star") as GameObject;
        for (int i = 0; i < (int)effect; i++)
        {
            GameObject obj = GameObject.Instantiate(starPrefab, transform);
            obj.name = "Star";
            obj.transform.position = transform.position + new Vector3(0.2f, -0.28f + i * 0.06f, -0.2f);
        }
        EffectObjectManager.Instance.CreateEffectObject("Prefabs/Effect/Effect_LevelUp", transform.position);
    }

    public override void BuffDown()
    {
        BattleObject bt = gameObject.GetComponent<BattleObject>();
        bt.setMaxHP(Mathf.Ceil(bt.maxHP / (1 + effect * 0.25f)));
        bt.damage = Mathf.Ceil(bt.damage / (1 + effect * 0.25f));
        UpdateSpeed(1f / (1 + effect * 0.1f));
        Transform[] trans = GetComponentsInChildren<Transform>();
        for (int i = 0; i < trans.Length; i++)
        {
            if (trans != null && trans[i].gameObject.name == "Star")
                Destroy(trans[i].gameObject);
        }
    }

    void UpdateSpeed(float effect)
    {
        Move move = gameObject.GetComponent<Move>();
        if (move != null)
            move.speed *= effect;
        BattleObject battle = gameObject.GetComponent<BattleObject>();
        if (battle != null)
        {
            battle.coolTime /= effect;
            battle.frontActionTime /= effect;
            battle.attackActionTime /= effect;
            battle.afterActionTime /= effect;
            battle.UpdateAnimatorSpeed();
        }
    }
}
